package com.example.musicplayer.service;

import com.example.musicplayer.bean.Music;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class MusicServiceProxy extends ServiceProxy implements IMusicService {

    private IMusicService mService = null;
    private Context mContext;

    private static MusicServiceProxy sInstance = null;

    private MusicServiceProxy(Context context) {
        mContext = context;
    }

    public static MusicServiceProxy getInstance(Context context) {
        if (sInstance != null) {
            return sInstance;
        }

        synchronized (MusicServiceProxy.class) {
            if (sInstance == null) {
                sInstance = new MusicServiceProxy(context);
            }
        }

        return sInstance;
    }

    public void connect(Intent intent, int flags) {
        super.connect(mContext, intent, flags);
    }

    @Override
    public boolean isConnected() {
        if (mService != null) {
            return true;
        }

        return false;
    }

    @Override
    public void onConnected(ComponentName name, IBinder service) {
        mService = IMusicService.Stub.asInterface(service);
    }

    @Override
    public void onDisconnected(ComponentName name) {
        mService = null;
    }

    @Override
    public IBinder asBinder() {
        return mService.asBinder();
    }

    @Override
    public void open(String path) throws RemoteException {
        if (isConnected()) {
            this.mService.open(path);
        }
    }

    @Override
    public boolean isPlaying() throws RemoteException {
        boolean result = false;
        if (isConnected()) {
            result = this.mService.isPlaying();
        }

        return result;
    }

    @Override
    public void pause() throws RemoteException {
        if (isConnected()) {
            this.mService.pause();
        }
    }

    @Override
    public void prev() throws RemoteException {
        if (isConnected()) {
            this.mService.prev();
        }
    }

    @Override
    public void next() throws RemoteException {
        if (isConnected()) {
            this.mService.next();
        }
    }

    @Override
    public void stop() throws RemoteException {
        if (isConnected()) {
            this.mService.stop();
        }
    }

    @Override
    public void play() throws RemoteException {
        if (isConnected()) {
            this.mService.play();
        }
    }

    @Override
    public long duration() throws RemoteException {
        long result = 0L;
        if (isConnected()) {
            result = this.mService.duration();
        }

        return result;
    }

    @Override
    public long position() throws RemoteException {
        long result = 0L;
        if (isConnected()) {
            result = this.mService.position();
        }

        return result;
    }

    @Override
    public void seek(long position) throws RemoteException {
        if (isConnected()) {
            this.mService.seek(position);
        }
    }

    @Override
    public void playList(long[] ids, int position) throws RemoteException {
        if (isConnected()) {
            this.mService.playList(ids, position);
        }
    }

    @Override
    public long[] getPlayingList() throws RemoteException {
        long[] result = null;
        if (isConnected()) {
            result = this.mService.getPlayingList();
        }

        return result;
    }

    @Override
    public void setCallback(IMusicServiceCallback cb) throws RemoteException {
        if (isConnected()) {
            this.mService.setCallback(cb);
        }
    }

    @Override
    public void setLogging(int level) throws RemoteException {
        if (isConnected()) {
            this.mService.setLogging(level);
        }
    }

    @Override
    public void playMusic(Music music) throws RemoteException {
        if (isConnected()) {
            this.mService.playMusic(music);
        }
    }

    @Override
    public void playMusics(Music[] musics) throws RemoteException {
        if (isConnected()) {
            this.mService.playMusics(musics);
        }
    }
}
